import TableData from "@/components/public/el-table"; //主页面列表
import messageBox from "@/components/public/messageBox"; //主页面分页
import TableLyout from '@/components/commonPublic/tableLyout.vue'
import {
    getOutsourcedtestingPageLists, //列表页接口
    getSpeciesDataList, //样品种类
    commitOutsourcedtesting, //提交
    resettingOutsourcedReport, //撤销
    rollbackOutsourcedReport //撤回
} from "@/api/request.js";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置
import actions from "@/actions";
import API from '@/utils/commonRequest.js'

export default {
    name: "outsourcedSample",
    data() {
        return {
            name: "outsourcedSample",
            userNature: null, //用户Nature
            showMsgBox: false,
            msgTip: "请先选中数据行！",
            msgType: 1,
            projectsObj: [], //项目名
            tendersObj: [],
            falgId: null,

            isLock: false,
            projectId: null,
            projectName: null,
            loading: false,
            btnObj: formJs.btnObj, //按钮
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表

            // 列表
            getList: {
                fprojectid: "",
                fCompanyid: "", //左侧选中组织机构id
                keyType: "", //上方查询条件材料种类
                keyWord: "", //上方查询条件样品名称
                page: 1, //当前页码
                pageSize: 15, //每页显示条数
                userId: "", //查询待办或已办时传 列表页查询不传
                modelType: "0", //0-待办 1-已办 传2 表示列表页查询
                startDates: "", //取样日期开始时间
                endDates: "", //取样日期结束时间
                timeDates: "",
                fSamplenumber: ""

            },
            isShow: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            activeIndex: "1",
            fId: null, //当前数据fId
            tableData: [],
            companyId: null,
            currentRowId: "",
            dialogTitle: null,

        };
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        messageBox

    },
    mounted() {
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
        }, true);
        API.userNature().then((res) => {
            if (res.errcode == 0) {
                this.userNature = res.data.Nature
            }
        }).catch((err) => {
            console.log(err)
        })
        // this.getList.fCompanyid = this.userInfo.companyId
        this.getDate()

    },
    methods: {

        confirmMsg() {
            this.showMsgBox = false;
        },
        reset(e) {
            this.getList = this.$options.data.call(this).getList
            this.getList.fprojectid = e
        },
        // 选择项目名称
        selectCompany(e) {
            this.companyName = e.F_ProjectName;
            this.getList.fDepartmentid = e.F_ProjectId;
            this.projectName = "";
            this.getList.fCompanyid = "";


            this.yj_searchForm(this.getList);
        },
        // 选择施工单位
        selectProject(e) {

            this.projectName = e.F_CompanyName;
            this.getList.fCompanyid = e.F_CompanyId;
            // this.yj_searchForm(this.getList);
        },
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                    headers: {
                        "Content-Type": "application/json",
                        Authorization: localStorage.getItem("access_token"),
                    },
                }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    this.tendersObj = [];
                    if (data.length == 0) {
                        this.$message.warning({
                            content: "有其他人员登录此账号！",
                            duration: 1000,
                        });
                        setTimeout(() => {
                            window.location.href = "http://172.18.5.201:1080/login";
                        }, 1200);
                    } else if (data.length == 1) {
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                        this.getList.fCompanyid = data[0].sectionId;
                    } else {
                        this.tendersObj = [{
                            fItemName: "所有标段",
                            fItemValue: "",
                        }, ];
                        data.map((item) => {
                            this.tendersObj.push({
                                fItemName: item.sectionName,
                                fItemValue: item.sectionId,
                            });
                        });
                    }
                }
            });
        },
        selectTender(e) {
            this.getList.stationId = "";
            this.getList.machineId = "";
            this.bhzDataList = [];
            this.bhjDataList = [];
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },

        // 新增/编辑
        yj_edit(num) {
            var data;
            if (num == 1) { // 编辑
                if (this.dataFid != null) {


                    if (this.currentRow.fChecktype == '监理外委') {
                        if (this.userNature == '中心试验室') {
                            if (this.currentRow.fOrdernumber == '' || this.currentRow.fStatus == '待送检') {


                                this.$router.push({
                                    path: "/outsourcedManage/outsourcedSampleText",
                                });
                                data = {
                                    data: this.currentRow,
                                    fCompanyid: this.getList.fCompanyid,
                                    status: num,
                                }
                            } else {

                                this.$message.warning({
                                    content: "请先选择未处理的数据",
                                    duration: 1000
                                });
                            }
                        } else {
                            this.$message.warning("该用户无权限");
                        }
                    } else if (this.currentRow.fChecktype == '施工外委') {
                        if (this.userNature == '施工单位') {
                            if (this.currentRow.fOrdernumber == '' || this.currentRow.fStatus == '待送检') {


                                this.$router.push({
                                    path: "/outsourcedManage/outsourcedSampleText",
                                });
                                data = {
                                    data: this.currentRow,
                                    fCompanyid: this.getList.fCompanyid,
                                    status: num,
                                }
                            } else {

                                this.$message.warning({
                                    content: "请先选择未处理的数据",
                                    duration: 1000
                                });
                            }
                        } else {
                            this.$message.warning("该用户无权限");
                        }
                    } else {
                        this.$message.warning("该用户无权限");
                    }
                } else {
                    this.$message.warning({
                        content: '请先选择需要编辑的数据',
                        duration: 1000
                    })

                }
            } else if (num == 2) {

                if (this.dataFid != null && this.currentRow.fStatus == '待送检') {
                    if (this.currentRow.fChecktype == '监理外委') {
                        if (this.userNature == '中心试验室') {
                            if (this.currentRow.fOrdernumber != "" && this.currentRow.fOrdernumber != null) {

                                this.confirmText('提交');
                            } else {

                                this.$message.warning({
                                    content: "请先选择正确的数据",
                                    duration: 1000
                                });
                            }
                        } else {
                            this.$message.warning("该用户无权限");
                        }
                    } else if (this.currentRow.fChecktype == '施工外委') {
                        if (this.userNature == '施工单位') {
                            if (this.currentRow.fOrdernumber != "" && this.currentRow.fOrdernumber != null) {
                                this.confirmText('提交');
                            } else {

                                this.$message.warning({
                                    content: "请先选择正确的数据",
                                    duration: 1000
                                });
                            }
                        } else {
                            this.$message.warning("该用户无权限");
                        }
                    } else {
                        this.$message.warning("该用户无权限");
                    }

                } else {

                    this.$message.warning({
                        content: "请先选择需要提交的数据",
                        duration: 1000
                    });

                }
            } else if (num == 7) { //查看

                if (this.dataFid != null) {
                    this.$router.push({
                        path: "/outsourcedManage/outsourcedSampleText",

                    });
                    data = {
                        data: this.currentRow,
                        fCompanyid: this.getList.fCompanyid,
                        status: num,

                    }

                } else {

                    this.$message.warning({
                        content: "请先选择需要查看的数据",
                        duration: 1000
                    });

                }
            } else if (num == 3) { //撤销
                // resettingOutsourcedReport
                if (this.dataFid != null) {
                    if (this.currentRow.fStatus == '已送检') {
                        // this.yj_revoke(this.currentRow)
                        this.confirmText('撤销');
                    } else {
                        this.$message.warning({
                            content: "请选择状态为'已送检'的数据",
                            duration: 1000
                        });

                    }

                } else {
                    this.$message.warning({
                        content: "请先选择需要撤销的数据",
                        duration: 1000
                    });
                }
            } else if (num == 4) { //撤回
                if (this.dataFid != null) {
                    if (this.currentRow.fStatus == '待送检') {
                        // this.yj_return(this.currentRow)
                        this.confirmText('撤销');

                    } else {
                        this.$message.warning({
                            content: "请选择状态为'待送检'的数据",
                            duration: 1000
                        });
                    }
                } else {

                    this.$message.warning({
                        content: "请先选择需要撤回的数据",
                        duration: 1000
                    });
                }
            }

            sessionStorage.setItem(
                "GxEmbedded_primaryInfo",
                JSON.stringify(data)
            );

        },
        editText() {
            this.isShow = true;
        },
        confirmText(status) {
            if (status == '删除') {
                this.msgTip = "此操作将永久删除该数据, 是否继续?";
                this.titelName = "删除";
            } else if (status == '提交') {
                this.msgTip = "是否继续提交?";
                this.titelName = "提交";
            } else if (status == '撤销') {
                this.msgTip = "是否确认撤销至待送检阶段?";
                this.titelName = "撤销";
            } else if (status == '撤回') {
                this.msgTip = "是否确认撤回至待取样阶段?";
                this.titelName = "撤回";
            }

            this.msgType = 2; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
            this.showMsgBox = true;

        },
        yj_revoke(row) {
            if (row.fId != "" && row.fId != null) {

           
                        this.loading = true;
                        resettingOutsourcedReport({
                                fId: row.fId,
                                userId: this.userInfo.userId,
                                userName: this.userInfo.realName,
                            })
                            .then((res) => {
                                if (res.errcode == 0) {
                                    this.loading = false;
                                    this.yj_searchForm(this.getList);
                                    this.$message.success("撤销成功");
                                    this.$message.success({
                                        content:"撤销成功",
                                        duration: 1000
                                    });
                                } else {
                                    this.$message.error({
                                        content:"撤销失败"+ res.msg,
                                        duration: 1000
                                    });
                                    this.loading = false;
                                }
                            })



            } else {

                this.$message.error({
                    content: "请先选择需要撤销的数据",
                    duration: 1000
                });
            }
        },
        yj_return(row) {


            this.loading = true;
            rollbackOutsourcedReport({
                    fId: row.fId,
                    userId: this.userInfo.userId,
                    userName: this.userInfo.realName,
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.yj_searchForm(this.getList);
           
                        this.$message.success({
                            content:"撤回成功",
                            duration: 1000
                        });
                    } else {
                        this.$message.success({
                            content:"撤回失败"+ res.msg,
                            duration: 1000
                        });
                        this.loading = false;
                    }
                })

        },
        // 带功能确定
        confirmMsgs() {
            this.showMsgBox = false;
            if (this.titelName == "提交") {
                this.yj_submit();
            } else if (this.titelName == "删除") {
                this.yj_delete();
            } else if (this.titelName == "撤销") {
                this.yj_revoke(this.currentRow);
            } else if (this.titelName == "撤回") {
                this.yj_return(this.currentRow);
            }
        },
        // 提交
        yj_submit() {
            if (this.currentRow != null) {
                if (this.dataFid != "" && this.dataFid != null) {

                    this.isLock = true
                    this.loading = true;
                    commitOutsourcedtesting({
                            fId: this.dataFid,
                            userId: this.userInfo.userId, //当前登录人id
                            userName: this.userInfo.realName //当前登录人姓名
                        })
                        .then((res) => {
                            if (res.errcode == 0) {
                                this.loading = false;
                                this.dataFid = null
                                this.isLock = false
                                this.yj_searchForm(this.getList);

                                this.$message.success({
                                    content: "提交成功",
                                    duration: 1000
                                });
                            } else {
                                this.isLock = false
                                this.$message.error({
                                    content: "提交失败" + res.msg,
                                    duration: 1000
                                });

                                this.loading = false;
                                return false;
                            }
                        })
                        .catch(() => {
                            this.loading = false;
                            console.log("提交失败");
                        });

                }
            }
        },
        // 查看
        // 点击查看
        yj_viewForm(row, label) {
            if (label == "取样编号") {
                this.currentRow = row;
                this.$router.push({
                    path: "/outsourcedManage/outsourcedSampleText",

                });
                var data = {
                    data: this.currentRow,
                    fCompanyid: this.getList.fCompanyid,
                    status: 7,

                }
                sessionStorage.setItem(
                    "GxEmbedded_primaryInfo",
                    JSON.stringify(data)
                );
            }
        },
        // 点击当前行变色
        rowChange(val) {
            this.currentRow = val
            this.dataFid = val.fId;
        },
        getSpeciesList() {
            var obj = {
                page: 1,
                pageSize: 500
            }
            getSpeciesDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {

                        this.searchObj[3].options = [{
                            label: "请选择材料种类",
                            value: "",
                        }]
                        res.data.pageInfo.list.map(item => {
                            if (item.fStatus == 1) {
                                this.searchObj[3].options.push({
                                    label: item.fVarietyname,
                                    value: item.fVarietyname,
                                    children: item.materialList
                                })
                            }
                        })

                    } else {
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        });

                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 获取列表数据
        yj_searchForm(data) {
            window.sessionStorage.setItem(
                "GxEmbedded-outsourcedManage_getList",
                JSON.stringify(data)
            );
            var id;
            if (data.fCompanyid != "") {
                id = data.fCompanyid;
            } else {
                id = data.fprojectid;
            }
            var obj = {
                // userId: "3a34a5db-5cbb-42c7-87a9-619321768c53", //查询待办或已办时传 列表页查询不传
                // fCompanyid: "d486cef5-67ad-42ae-bf7f-d3a104760bd2",
                fCompanyid: id, //左侧选中组织机构id
                keyType: data.keyType, //上方查询条件材料种类
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数


                userId: this.userInfo.id, //查询待办或已办时传 列表页查询不传
                modelType: 3, //0-待办 1-已办 传2 表示列表页查询
                startDates: data.startDates, //进场开始时间
                endDates: data.endDates, //进场结束时间
                keyWord: data.keyWord, //上方查询条件样品名称
                fSamplenumber: data.fSamplenumber

            }
            this.loading = true;
            var that = this;
            getOutsourcedtestingPageLists(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error({
                            content: "列表数据获取失败" + res.msg,
                            duration: 1000
                        });
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error({
                        content: "列表数据获取失败" + error.msg,
                        duration: 1000
                    });
                    this.loading = false;
                });
        },
        selectTime(e) {
            if (e[0] != "" && e[1] != "") {
                this.getList.startDates = e[0]; // 开始时间
                this.getList.endDates = e[1]; // 截至时间
            } else {
                this.getList.startDates = ""; // 开始时间
                this.getList.endDates = ""; // 截至时间
            }
            this.getList.timeDates = [e[0], e[1]]
            this.yj_searchForm(this.getList);

        },
        getDate() {
            this.getSpeciesList();
            if (JSON.parse(sessionStorage.getItem("GxEmbedded-outsourcedManage_getList"))) {
                var data = JSON.parse(sessionStorage.getItem("GxEmbedded-outsourcedManage_getList"));
                if (data.fprojectid != "") {
                    this.getTender(data.fprojectid);
                } else {
                    data.fprojectid = this.projectsObj[1].fItemValue;
                    this.getTender(data.fprojectid);
                }
                this.getList = {
                    fprojectid: data.fprojectid,
                    fCompanyid: data.fCompanyid, //左侧选中组织机构id
                    keyType: data.keyType, //上方查询条件材料种类
                    taskNumber: '', //上方查询条件进场编号
                    page: 1, //当前页码
                    pageSize: 15, //每页显示条数
                    userId: this.userInfo.id, //查询待办或已办时传 列表页查询不传
                    modelType: 3, //0-待办 1-已办 传2 表示列表页查询
                    startDates: data.startDates, //进场开始时间
                    endDates: data.endDates, //进场结束时间
                    timeDates: [data.startDates, data.endDates],
                    fSamplenumber: data.fSamplenumber

                };
            } else {
                this.getList.startDates = this.common.getDay(-90); // 开始时间
                this.getList.endDates = this.common.getDay(0); // 截至时间
                this.getList.timeDates = [this.getList.startDates, this.getList.endDates]
            }
        },

    },
};